package com.shopping.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.shopping.product.entity.ProductAttrValueEntity;
import com.shopping.product.service.AttrService;
import com.shopping.product.service.ProductAttrValueService;
import com.shopping.product.vo.AttrGroupRelationVo;
import com.shopping.product.vo.AttrRespVo;
import com.shopping.product.vo.AttrVo;
import org.springframework.web.bind.annotation.*;

import com.shopping.product.entity.AttrEntity;
import com.shopping.common.utils.PageUtils;
import com.shopping.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性
 *
 * @author Europa
 * @email qw1124sdo8@live.com
 * @date 2020-04-11 22:49:19
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Resource
    private AttrService attrService;

    @Resource
    ProductAttrValueService productAttrValueService;


    /**
     * 修改规格维护
     *
     * @param spuId    spu_id
     * @param entities 含ProductAttrValueEntity的list
     * @return 状态码 + 操作信息
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> entities) {
        productAttrValueService.updateSpuAttr(spuId, entities);
        return R.ok();
    }

    /**
     * 规格维护查询
     *
     * @param spuId spu_id
     * @return 状态码 + 操作信息 + 含ProductAttrValueEntity的list
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", entities);
    }

    /**
     * 根据{attrType}选择 获取分类规格参数 或 获取分类销售属性
     *
     * @param params map{t=时间戳, page=页码, limit=每页内容条数, key=检索关键字}
     * @param catelogId catelog_id
     * @param type      sale/base 销售/基础 类型
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查询属性详情
     *
     * @param attrId AttrEntity的属性id
     * @return 状态码 + 操作信息 + AttrRespVo实体
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
//        AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }


    /**
     * 保存属性【规格参数，销售属性】
     *
     * @param attr
     * @return
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改属性
     *
     * @param attr AttrVo实体
     * @return 状态码 + 操作信息
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:updateSort")
    public R update(@RequestBody AttrVo attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
